﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.FoodTesting.CarbohydratesFood
{
    public partial class CarbohydratesEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_CarbohydratesData tCarbohydratesData = new T_CarbohydratesData();              //检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_CarbohydratesData md_WaterData = new E_CarbohydratesData();
            md_WaterData.CarbohydratesTime = date;
            md_WaterData.RawId = RawId;
            List<E_CarbohydratesData> eCarbohydratesData = tCarbohydratesData.GetDataListByTime(md_WaterData);

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 3;//碳水化合物
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            if (eCarbohydratesData != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_CarbohydratesData md = eCarbohydratesData.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (md != null)
                {
                    this.txt_XGDJZ.Text = md.Carbohydrates_XGDJZ;
                    this.txt_m1.Text = md.Carbohydrates_m1;
                    this.txt_m.Text = md.Carbohydrates_m;
                    this.txt_V.Text = md.Carbohydrates_v;

                    this.txt_F.Text = md.Carbohydrates_F;
                    this.txt_V1.Text = md.Carbohydrates_V1;
                    this.txt_V2.Text = md.Carbohydrates_V2;
                    this.txt_x.Text = md.Carbohydrates_X;
                }
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                string XGDJZ = this.txt_XGDJZ.Text.Trim();
                double m1 = Convert.ToDouble(this.txt_m1.Text.Trim());
                double m = Convert.ToDouble(this.txt_m.Text.Trim());
                double v = Convert.ToDouble(this.txt_V.Text.Trim());
                double x = (m1 / (m * v / 250 * 1000)) * 100;

                Convert.ToInt32(x);

                double F = Convert.ToDouble(this.txt_F.Text.Trim());
                double V1 = Convert.ToDouble(this.txt_V1.Text.Trim());
                double V2 = Convert.ToDouble(this.txt_V2.Text.Trim());
                double ZT = (F / m * (50 / V1) * (V2 / 100) * 1000) * 100;

                Convert.ToInt32(ZT);
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);

                E_CarbohydratesData eCarbohydratesData = new E_CarbohydratesData();
                eCarbohydratesData.RawId = RawId;
                eCarbohydratesData.RawName = eRawMaterial.RAWName;
                eCarbohydratesData.Carbohydrates_XGDJZ = XGDJZ;
                eCarbohydratesData.Carbohydrates_X = x.ToString("f2");
                eCarbohydratesData.Carbohydrates_m1 = m1.ToString();
                eCarbohydratesData.Carbohydrates_m = m.ToString();
                eCarbohydratesData.Carbohydrates_v = v.ToString();
                eCarbohydratesData.Carbohydrates_F = F.ToString();
                eCarbohydratesData.Carbohydrates_ZT = ZT.ToString("f2");
                eCarbohydratesData.Carbohydrates_V1 = V1.ToString();
                eCarbohydratesData.Carbohydrates_V2 = V2.ToString();
                eCarbohydratesData.CarbohydratesTime = date;
                eCarbohydratesData.FoodNum = FoodNum;

                tCarbohydratesData.RemoveByFoodNum(eCarbohydratesData);   //删除现有的检测记录
                tCarbohydratesData.Add(eCarbohydratesData);               //添加检测记录
                this.txt_x.Text = x.ToString("f2");
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_CarbohydratesData md_CarbohydratesData = new E_CarbohydratesData();
            md_CarbohydratesData.CarbohydratesTime = date;
            md_CarbohydratesData.RawId = RawId;
            List<E_CarbohydratesData> eVcDataList = tCarbohydratesData.GetDataListByTime(md_CarbohydratesData);


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 3;//碳水化合物
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eVcDataList == null || eVcDataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            eVcDataList.ForEach(p =>
            {
                Result += Convert.ToDouble(p.Carbohydrates_ZT);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}